<template>
    <div class="content">
        <el-dialog :title="this.addOrUpdate? '新增代码模板' : '编辑代码模板'" :close-on-click-modal="false" :visible.sync="visible" width="30%">
            <el-form ref="form" :model="form" label-width="auto">
                <el-form-item label="命名空间ID">
                    <el-input v-model="form.ns" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="数据源名称">
                    <el-input v-model="form.ds" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="模板名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="包路径">
                    <el-input v-model="form.packageReference"></el-input>
                </el-form-item>
                <el-form-item label="模块目录">
                    <el-input v-model="form.moduleName"></el-input>
                </el-form-item>
                <el-form-item label="作者">
                    <el-input v-model="form.author"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="表前缀">
                    <el-input v-model="form.tablePrefix"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-circle-plus" @click="handleSumbit">提交</el-button>
                    <el-button icon="el-icon-error" @click="visible = false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

    </div>
</template>
<script>
    import { templateAdd, templateUpdate, templateInfo } from '../../api/template'
    export default {
        data() {
            return {
                addOrUpdate: false,
                visible: false,
                form: {
                    id: null,
                    ns: '',
                    ds: '',
                    name: '',
                    packageReference: '',
                    moduleName: '',
                    author: '',
                    email: '',
                    tablePrefix: ''
                }
            }
        },
        methods: {
            init(addOrUpdate, ns, ds, id) {
                this.form = {
                    id: null,
                    ns: '',
                    ds: '',
                    name: '',
                    packageReference: '',
                    moduleName: '',
                    author: '',
                    email: '',
                    tablePrefix: ''
                }
                this.form.ns = ns || 'public'
                this.form.ds = ds || ''
                this.form.id = id || 0
                this.addOrUpdate = addOrUpdate
                this.visible = true
                this.$nextTick(() => {
                    this.$refs.form.resetFields()
                    if (!this.addOrUpdate) {
                        templateInfo(this.form.id).then(res => {
                            this.form = res.data
                        })
                    }
                })
            },
            handleSumbit() {
                if (this.addOrUpdate) {
                    templateAdd(this.form).then((res) => {
                        this.$message({
                            message: '新增成功！', type: 'success', duration: 800, onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                            }
                        })
                    })
                } else {
                    templateUpdate(this.form).then(res => {
                        this.$message({
                            message: '更新成功！', type: 'success', duration: 800, onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                            }
                        })
                    })
                }
            }
        }
    }
</script>
<style scoped>
.content {
    margin: 20px 0px;
    font-size: 1em;
}
</style>
